import 'dart:io';

import 'package:card_topia/design_specifications.dart';
import 'package:card_topia/my_router.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

///自定义弹窗
class DialogUtil {
  static showLoading() {
    EasyLoading.show(
        indicator: const CupertinoActivityIndicator(
      color: Colors.white,
    ));
  }

  static dismiss() {
    EasyLoading.dismiss();
  }

  //toast
  static showToast({required String text}) {
    // EasyLoading.instance.loadingStyle = EasyLoadingStyle.custom;
    // EasyLoading.instance.maskColor = Colors.transparent;
    // EasyLoading.instance.backgroundColor = Color(0xFF358DED);
    // EasyLoading.instance.textColor = Colors.white;
    EasyLoading.instance.textStyle =
        TextStyle(fontSize: 14.sp, fontFamily: harmony, color: Colors.white);
    EasyLoading.showToast(text);
  }

  //居中提示
  static shwCenterDialog(BuildContext context,{required String title,required String content,required bool singleBtn,String? confirmLabel,Function? onConfirm}){
    showCupertinoDialog(context: context, builder: (_)=>Center(
      child: Container(
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.circular(8.w)
        ),
        width: double.infinity,
        margin: EdgeInsets.symmetric(horizontal: 32.w),
        padding: EdgeInsets.symmetric(horizontal: 16.w),
        child: SingleChildScrollView(
          child: Column(
            children: [
              SizedBox(height: 16.w,),
              Text(title,style: TextStyle(fontSize: 18.sp,fontFamily: harmony,fontWeight: FontWeight.bold,color: textColor),),
              Container(
                  margin: EdgeInsets.symmetric(vertical: 32.w),
                  alignment: Alignment.center,
                  child: Text(content,style: TextStyle(fontSize: 16.sp,color: descColor,fontFamily: harmony),)),
              Divider(color: disableColor.withOpacity(0.2),indent: 0,endIndent: 0,thickness: 0.5,),
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceAround,
                children: [
                  Expanded(
                    child: InkWell(
                      onTap:()=>Navigator.pop(context),
                      child: Container(
                          padding: EdgeInsets.only(top: 12.w,bottom: 16.w),
                          alignment: Alignment.center,
                          child: Text(singleBtn?"知道了":"取消",style: TextStyle(fontSize: 16.sp,color: singleBtn?Colors.red:disableColor,fontFamily: harmony),)),
                    ),
                  ),
                  if(!singleBtn)
                  Expanded(
                    child: InkWell(
                      onTap:(){
                        Navigator.pop(context);
                        if(onConfirm!=null){
                          onConfirm();
                        }
                      },
                      child: Container(
                          padding: EdgeInsets.only(top: 12.w,bottom: 16.w),
                          alignment: Alignment.center,
                          child: Text(confirmLabel??"",style: TextStyle(fontSize: 16.sp,color: themeColor,fontFamily: harmony),)),
                    ),
                  ),
                ],
              )
            ],
          ),
        ),
      ),
    ));
  }
  //自定义dialog
  static Future<bool?> showDefaultDialog(BuildContext context,
      {String? title,
      String? content,
      bool? isDoubleBtn,
      String? cancel,
      Function? onCancel,
      String? confirm,
        Color? conformColor,
      Function? onConfirm}) {
    return showCupertinoModalPopup(
        context: context,
        barrierDismissible: false,
        builder: (_) => Container(
              decoration: BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.circular(20.w),
              ),
              width: double.infinity,
              padding: EdgeInsets.only(
                  left: 16.w,
                  right: 16.w,
                  bottom: Platform.isIOS
                      ? MediaQuery.of(context).padding.bottom+15.w
                      : 15.w,
                  top: 12.w),
              child: SingleChildScrollView(
                child: Column(
                  children: [
                    if (title != null)
                      Container(
                        alignment: Alignment.center,
                        margin: EdgeInsets.only(top: 12.w,bottom: 12.w),
                        child: Text(
                          title ?? "",
                          style:
                              TextStyle(fontSize: 16.sp, fontFamily: harmony),
                        ),
                      ),
                    if (content != null)
                      Container(
                        padding: EdgeInsets.symmetric(vertical: 12.w),
                        alignment: Alignment.center,
                        child: Text(
                          content ?? "",
                          style: TextStyle(
                              fontSize: 14.sp,
                              color: descColor,
                              fontFamily: harmony),
                        ),
                      ),
                    SizedBox(height: 24.w,),
                    InkWell(
                        onTap: (){
                          // context.pop(false);
                          MyRouter.pop(false);
                          if(onConfirm!=null){
                            onConfirm();
                          }
                        },
                        child: Container(
                          width: double.infinity,
                            padding: EdgeInsets.symmetric(vertical:12.w),
                            alignment: Alignment.center,
                            child: Text(confirm??"确定",style: TextStyle(fontSize: 16.sp,color: conformColor??Colors.red),))),
                    SizedBox(height: 12.w,),
                    InkWell(
                        onTap: () => MyRouter.pop(),
                        child: Container(
                          width: double.infinity,
                            padding: EdgeInsets.symmetric(vertical:12.w),
                            alignment: Alignment.center,
                            child: Text(cancel??"取消",style: TextStyle(fontFamily: harmony,fontSize: 16.sp,color: disableColor),))),
                  ],
                ),
              ),
            ));
  }
}
